/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.chooser.view;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import offset.nodes.client.model.ActivePanelContainer;
import offset.nodes.client.model.NodeTypes;
import offset.nodes.client.model.Server;
import offset.nodes.client.virtual.model.GetSchemas;
import offset.nodes.client.virtual.model.jcr.QName;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeDefinition;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeReader;
import offset.nodes.client.view.SimpleDialog;

/**
 *
 * @author Walter Lütgenau
 */
public class TypeChooserDialog extends SimpleDialog implements ActivePanelContainer {
    TypeChooserPanel panel;

    public TypeChooserDialog(JFrame parent, boolean modal, Server model) {
        super(parent, modal);
        addContent(panel = new TypeChooserPanel(new NodeTypes(getDefinitions(model))));
        init();
    }

    public TypeChooserDialog(JFrame parent, boolean modal, Server model, QName uriFilter) {
        super(parent, modal);
        addContent(panel = new TypeChooserPanel(new NodeTypes(getDefinitions(model)), uriFilter));
        init();
    }

    public TypeChooserDialog(JFrame parent, boolean modal, NodeTypeDefinition[] definitions) {
        super(parent, modal);
        addContent(panel = new TypeChooserPanel(new NodeTypes(definitions)));
        init();
    }

    protected void init() {
        setSize(500, 300);
        enableOkButton(false);
        panel.setContainer(this);
        panel.initFocus();
    }

    public String getSelectedType() {
        return panel.getSelectedType();
    }

    public void setSelectedType(String type) {
        panel.setSelectedType(type);
    }

    public NodeTypeDefinition getDefinition() {
        return panel.getSelectedDefinition();
    }

    public NodeTypeDefinition[] getDefinitions(Server model) {
        try {
            GetSchemas.Request request = new GetSchemas.Request();
            GetSchemas.Response response = null;
            response = (GetSchemas.Response) model.sendRequest(request);
            InputStream in = new ByteArrayInputStream(response.getSchemaXml().getBytes());
            NodeTypeDefinition[] definitions = NodeTypeReader.read(in);
            return definitions;
        } catch (Exception ex) {
            Logger.getLogger(TypeChooserDialog.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
